<template>
   <div class="role-manage">

       <div class="base-table">
           <div class="action">
               <el-button type="primary" @click="handleCreate">新增</el-button>
           </div>
           <el-table :data="userList" @selection-change="handleSelectionChange">
               <el-table-column type="selection" width="25" />
                <el-table-column 
                v-for="item in columns"
                :key="item.prop"
                :prop="item.prop" :label="item.label" :width="item.width" 
                :formatter="item.formatter"/>

                <el-table-column label="操作" width="180">
                    <template #default="scope">
                        <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-button type="text" size="small" @click="handleDel(scope.row)">删除</el-button>
                        <el-button type="text" size="small" @click="handlePre(scope.row)">设置权限</el-button>
                    </template>
                    </el-table-column>
            </el-table>
            <el-pagination class="pagination" background layout="prev, pager, next" 
            :total="pager.total"
            :page-size="pager.pageSize"
            @current-change="handleCurrentChange"
            ></el-pagination>
       </div>

       <el-dialog title="角色新增" v-model="showModule" width="30%">
           <el-form ref="userAddform" :model="userAdd" 
           label-width="80px" 
           :rules="rulesAdd">
               <el-form-item label="角色名称" prop="name">
                   <el-input v-model="userAdd.name" placeholder="请输入角色名称"/>
               </el-form-item>
               <el-form-item label="编号" prop="code">
                   <el-input v-model="userAdd.code" placeholder="请输入编号"/>
               </el-form-item>
           </el-form>

           <template #footer>
                <span class="dialog-footer">
                    <el-button @click="handleAddReset">取消</el-button>
                    <el-button type="primary" @click="handleAddUser">确认</el-button>
                </span>
            </template>
       </el-dialog>

       <el-dialog title="角色权限设置" v-model="showPremModule" width="30%">
           <el-form label-width="80px">
               <el-form-item label="角色名称" >
                   {{rolePrem.roleName}}
               </el-form-item>
               <el-form-item label="权限树" >
                   <el-tree
                        ref="premTree"
                        :data="menuList"
                        show-checkbox
                        node-key="id"
                        default-expand-all
                    />
               </el-form-item>
           </el-form>

           <template #footer>
                <span class="dialog-footer">
                    <el-button @click="handlePermReset">取消</el-button>
                    <el-button type="primary" @click="handleAddPrem">确认</el-button>
                </span>
            </template>
       </el-dialog>
   </div>
</template>
<script>
import {getCurrentInstance, onMounted, reactive, ref} from 'vue'
import { ElMessage } from 'element-plus'
export default {
    name:'user',
    setup(){
        // 生产环境下的ctx将访问不到
        const { proxy, ctx } = getCurrentInstance();
        const columns = reactive([
            {
                label:'ID',
                prop:'id'
            },
            {
                label:'角色名称',
                prop:'name'
            },
            {
                label:'编号',
                prop:'code'
            },
            {
                label:'创建时间',
                prop:'createTime'
            }
        ]);
        const user = reactive({});
        const userAdd = reactive({enable:true});
        const userList = ref([]);
        const menuList = ref([]);
        const pager = reactive({
            page:1,
            pageSize:10
        })
        onMounted(()=>{
            getUserList();
            getMenuList();
        })
        const getMenuList = async ()=>{
            const { code, result, message } = await proxy.$api.menuTree()
            menuList.value = result;
        }
        const getUserList = async ()=>{
            let params = { ...user, ...pager };
            const { code, result, message } = await proxy.$api.roleList(params)
            const {list, pageEl} = result;
            userList.value = list;
            pager.total = pageEl.total;
        }

        const handleQuery = ()=>{
            getUserList();
        }
        const handleReset = ()=>{
            ctx.$refs.userForm.resetFields();
        }
        const handleCurrentChange = (current)=>{
            pager.page = current;
            getUserList()
        }
        const handleDel = async (row)=>{
            await proxy.$api.roleDel({
                ids:row.id
            })
            ElMessage.success('删除成功')
            getUserList()
        }
        const handleSelectionChange = (list)=>{
            let arrIds = [];
            list.map(item=>{
                arrIds.push(item.id)
            })
        }
        const showModule = ref(false);
        const handleCreate = ()=>{
            showModule.value = true;
        }
        const rulesAdd = reactive({
            userName:[
                {
                    required:true,
                    message:'请输入用户名',
                    trigger:'blur'
                }
            ],
            email:[
                {
                    required:true,
                    message:'请输入邮箱',
                    trigger:'blur'
                }
            ]
        })
        const handleAddReset = ()=>{
            ctx.$refs.userAddform.resetFields();
            showModule.value = false;
        }
        const handleAddUser = async ()=>{
            const {code,message} = await proxy.$api.roleAdd(userAdd)
            if(code === 200){
                ElMessage.success(message)
                showModule.value = false;
                handleAddReset()
                getUserList()
            }
        }
        const handleEdit = async (row)=>{
            showModule.value = true;
            ctx.$nextTick(()=>{
                Object.assign(userAdd, row);
            });
        }
        const showPremModule = ref(false);
        const rolePrem = ref({
            roleName:'',
            roleId:0,
            moduleIds:''
        });
        const handlePre = async (row)=>{
            rolePrem.value.roleName = row.name;
            rolePrem.value.roleId = row.id;
            showPremModule.value = true;
            setTimeout(() => {
                ctx.$refs.premTree.setCheckedKeys(row.menuKeys || []);
            });
        }
        const handlePermReset = ()=>{
            rolePrem.value = {};
            ctx.$refs.premTree.setCheckedKeys([]);
            showPremModule.value = false;
        }
        const handleAddPrem = async ()=>{
            let menuIds = ctx.$refs.premTree.getCheckedKeys();
            rolePrem.value.moduleIds = menuIds.join();
            // console.log('---------rolePrem-----------' + rolePrem)
            const {code,message} = await proxy.$api.roleAddPrem(rolePrem.value);
            if(code === 200){
                ElMessage.success(message);
                handlePermReset();
                getUserList();
            }
        }
        return{
            user,
            userAdd,
            userList,
            columns,
            pager,
            showModule,
            rulesAdd,
            showPremModule,
            rolePrem,
            menuList,
            getUserList,
            handleQuery,
            handleReset,
            handleCurrentChange,
            handleDel,
            handleSelectionChange,
            handleCreate,
            handleAddReset,
            handleAddUser,
            handleEdit,
            handlePre,
            handlePermReset,
            handleAddPrem
        }
    }
}
</script>
<style lang="scss">

</style>